package com.cdtye.shuoh.config.gb28181.transmit;

import com.cdtye.shuoh.config.gb28181.config.SipConfig;
import com.cdtye.shuoh.config.gb28181.sip.auth.RegisterLogicHandler;
import com.cdtye.shuoh.config.gb28181.sip.event.DeviceOffLineDetector;
import com.cdtye.shuoh.config.gb28181.sip.event.EventPublisher;
import com.cdtye.shuoh.config.gb28181.storage.IRedisCatchStorage;
import com.cdtye.shuoh.config.gb28181.transmit.callback.DeferredResultHolder;
import com.cdtye.shuoh.config.gb28181.transmit.cmd.impl.SIPCommander;
import com.cdtye.shuoh.config.gb28181.transmit.request.ISIPRequestProcessor;
import com.cdtye.shuoh.config.gb28181.transmit.request.impl.*;
import com.cdtye.shuoh.config.gb28181.transmit.response.ISIPResponseProcessor;
import com.cdtye.shuoh.config.gb28181.transmit.response.impl.ByeResponseProcessor;
import com.cdtye.shuoh.config.gb28181.transmit.response.impl.CancelResponseProcessor;
import com.cdtye.shuoh.config.gb28181.transmit.response.impl.InviteResponseProcessor;
import com.cdtye.shuoh.config.gb28181.transmit.response.impl.OtherResponseProcessor;
import com.cdtye.shuoh.config.redis.RedisService;
import com.cdtye.shuoh.service.device.DeviceChannelService;
import com.cdtye.shuoh.service.device.DeviceService;
import com.cdtye.shuoh.util.Spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.sip.RequestEvent;
import javax.sip.ResponseEvent;
import javax.sip.SipProvider;
import javax.sip.header.CSeqHeader;
import javax.sip.message.Request;
import javax.sip.message.Response;

// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;

/**    
 * @Description: SIP信令处理分配   
 * @author: swwheihei
 * @date:   2020年5月3日 下午4:24:37     
 */
@Component
@ConditionalOnProperty(value = "sip.enabled", havingValue = "true")
public class SIPProcessorFactory {
	
	 private final static Logger logger = LoggerFactory.getLogger(SIPProcessorFactory.class);
	
	@Autowired
	private SipConfig sipConfig;
	
	@Autowired
	private RegisterLogicHandler handler;

	@Autowired
	private DeviceService deviceService;

	@Autowired
	private DeviceChannelService deviceChannelService;

	@Autowired
	private IRedisCatchStorage redisCatchStorage;
	
	@Autowired
	private EventPublisher publisher;
	
	@Autowired
	private SIPCommander cmder;
	
	@Autowired
	private RedisService redis;
	
	@Autowired
	private DeferredResultHolder deferredResultHolder;
	
	@Autowired
	private DeviceOffLineDetector offLineDetector;
	
	@Autowired
	private InviteResponseProcessor inviteResponseProcessor;
	
	@Autowired
	private ByeResponseProcessor byeResponseProcessor;
	
	@Autowired
	private CancelResponseProcessor cancelResponseProcessor;
	
	@Autowired
	private OtherResponseProcessor otherResponseProcessor;


	// 注：这里使用注解会导致循环依赖注入，暂用springBean
	private SipProvider tcpSipProvider;
		
	// 注：这里使用注解会导致循环依赖注入，暂用springBean
	private SipProvider udpSipProvider;
	
	public ISIPRequestProcessor createRequestProcessor(RequestEvent evt) {
		Request request = evt.getRequest();
		String method = request.getMethod();
		logger.info("接收到消息request："+request.getMethod());
		if (Request.INVITE.equals(method)) {
			InviteRequestProcessor processor = new InviteRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setTcpSipProvider(getTcpSipProvider());
			processor.setUdpSipProvider(getUdpSipProvider());
			return processor;
		} else if (Request.REGISTER.equals(method)) {
			RegisterRequestProcessor processor = new RegisterRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setTcpSipProvider(getTcpSipProvider());
			processor.setUdpSipProvider(getUdpSipProvider());
			processor.setHandler(handler);
			processor.setPublisher(publisher);
			processor.setSipConfig(sipConfig);
			processor.setDeviceService(deviceService);
			processor.setDeviceChannelService(deviceChannelService);
			return processor;
		} else if (Request.SUBSCRIBE.equals(method)) {
			SubscribeRequestProcessor processor = new SubscribeRequestProcessor();
			processor.setRequestEvent(evt);
			return processor;
		} else if (Request.ACK.equals(method)) {
			AckRequestProcessor processor = new AckRequestProcessor();
			processor.setRequestEvent(evt);
			return processor;
		} else if (Request.BYE.equals(method)) {
			ByeRequestProcessor processor = new ByeRequestProcessor();
			processor.setRequestEvent(evt);
			return processor;
		} else if (Request.CANCEL.equals(method)) {
			CancelRequestProcessor processor = new CancelRequestProcessor();
			processor.setRequestEvent(evt);
			return processor;
		} else if (Request.MESSAGE.equals(method)) {
			MessageRequestProcessor processor = new MessageRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setTcpSipProvider(getTcpSipProvider());
			processor.setUdpSipProvider(getUdpSipProvider());
			processor.setPublisher(publisher);
			processor.setRedis(redis);
			processor.setDeferredResultHolder(deferredResultHolder);
			processor.setOffLineDetector(offLineDetector);
			processor.setCmder(cmder);
			processor.setDeviceService(deviceService);
			processor.setDeviceChannelService(deviceChannelService);
			processor.setRedisCatchStorage(redisCatchStorage);
			return processor;
		}
//		else if (Request.NOTIFY.equalsIgnoreCase(method)) {
//			NotifyRequestProcessor processor = new NotifyRequestProcessor();
//			processor.setRequestEvent(evt);
//			processor.setTcpSipProvider(getTcpSipProvider());
//			processor.setUdpSipProvider(getUdpSipProvider());
//			processor.setPublisher(publisher);
//			processor.setRedis(redis);
//			processor.setDeferredResultHolder(deferredResultHolder);
//			processor.setOffLineDetector(offLineDetector);
//			processor.setCmder(cmder);
//			processor.setStorager(storager);
//			processor.setRedisCatchStorage(redisCatchStorage);
//			return processor;
//		}
			else {
			OtherRequestProcessor processor = new OtherRequestProcessor();
			processor.setRequestEvent(evt);
			return processor;
		}
	}
	
	public ISIPResponseProcessor createResponseProcessor(ResponseEvent evt) {
		Response response = evt.getResponse();
		CSeqHeader cseqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
		String method = cseqHeader.getMethod();
		logger.info("接收到消息response："+method);
		if(Request.INVITE.equals(method)){
			return inviteResponseProcessor;
		} else if (Request.BYE.equals(method)) {
			return byeResponseProcessor;
		} else if (Request.CANCEL.equals(method)) {
			return cancelResponseProcessor;
		} else {
			return otherResponseProcessor;
		}
	}
	
	private SipProvider getTcpSipProvider() {
		if (tcpSipProvider == null) {
			tcpSipProvider = (SipProvider) SpringUtils.get("tcpSipProvider");
		}
		return tcpSipProvider;
	}
	
	private SipProvider getUdpSipProvider() {
		if (udpSipProvider == null) {
			udpSipProvider = (SipProvider) SpringUtils.get("udpSipProvider");
		}
		return udpSipProvider;
	}
	
}
